<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
</head>

<body>
    <div class="nav">
        <img src="image/image1.jpg" alt="">
        <span class="title">个人博客系统</span>
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="logout" onclick="logout()">注销</a>
    </div>

    <div class="container">
        <div class="left">
            <div class="card1">
                <img src="image/image1.jpg" alt="">
                <h3></h3>
                <a href="">github 地址</a>
                <div class="blogcontent">
                    <span>文章阅读量: </span>
                </div>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <div class="right">
            <div class="blog-content">
                <h3></h3>
                <div class="date">
                </div>
                <div id='content' style="background-color:transparent;">
                </div>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script>

        var blogId = getURLParam("blogId");
        if (blogId == null || blogId == "" && blogId <= 0) {
            alert("访问失败，非法参数");
            location.href("index.html");
        } else {
            //加载文章
            loadBlog();
            //加载作者
            loadUserInfo();
            //文章数加一
            addRCount()
        }

        function loadBlog() {
            jQuery.ajax({
                url: 'blog/getblog',
                type: 'POST',
                data: { "blogId": blogId },
                success: function (result) {
                    if (result != null && result.success == 200) {
                        if (result.data != null && result.data.blogId >= 0) {
                            var blog = result.data
                            let titleDiv = document.querySelector('.blog-content>h3');
                            titleDiv.innerHTML = blog.title;
                            let dateDiv = document.querySelector('.blog-content>.date');
                            dateDiv.innerHTML = formatDate(blog.postTime);
                            editormd.markdownToHTML('content', { markdown: blog.content });
                            //文章阅读量
                            let blogcontent = document.createElement('span');
                            //blogcontent.className = 'blogcontent';
                            blogcontent.innerHTML = blog.readCount;
                            let blogcontent1 = document.querySelector('.card1>.blogcontent');
                            blogcontent1.appendChild(blogcontent);
                        } else {
                            alert("抱歉，当前文章不存在！");
                        }
                    } else {
                        alert("抱歉，后端程序不可用");
                    }
                }
            });
        }

        function formatDate(timeStampMS) {
            var date = new Date(timeStampMS);

            var year = date.getFullYear(),
                month = date.getMonth() + 1,//月份是从0开始的
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();
            var newTime = year + '-' +
                (month < 10 ? '0' + month : month) + '-' +
                (day < 10 ? '0' + day : day) + ' ' +
                (hour < 10 ? '0' + hour : hour) + ':' +
                (min < 10 ? '0' + min : min) + ':' +
                (sec < 10 ? '0' + sec : sec);
            return newTime;
        }
        function loadUserInfo() {
            jQuery.ajax({
                url: 'user/getuserbyblogid',
                type: 'GET',
                data: { 'blogId': blogId },
                success: function (result) {
                    if (result != null && result.success == 200) {
                        if (result.data != null && result.data.userId > 0) {
                            changeUser(result.data);
                        } else {
                            alert("抱歉：后端程序异常，" + result.msg);
                        }
                    } else {
                        alert("后端程序不可用!");
                    }
                }
            });
        }

        function changeUser(user) {
            let h3 = document.querySelector('.card1>h3');
            h3.innerHTML = user.username;
            //git链接
            let gita = document.querySelector('.card1>a');
            gita.innerHTML = user.github;
            gita.href = user.github;

            //添加用户管理
            let navDiv = document.querySelector('.nav');
            let userEditButton = document.createElement('a');
            userEditButton.innerHTML = '用户管理';
            userEditButton.href = 'user_edit.html?userId=' + user.userId;
            navDiv.appendChild(userEditButton);
            jQuery.ajax({
                url: 'blog/isyourblog',
                type: 'GET',
                data: {
                    "userId": user.userId,
                    "blogId": blogId
                },
                success: function (result) {
                    if (result != null && result.success == 200) {
                        if (result.data != null && result.data == 1) {
                            let navDiv = document.querySelector('.nav');
                            //添加博客删除
                            let delButton = document.createElement('a');
                            delButton.innerHTML = '删除';
                            delButton.href = '#';
                            delButton.onclick = deleteBlog;
                            navDiv.appendChild(delButton)
                            //添加博客编辑
                            let editButton = document.createElement('a');
                            editButton.innerHTML = '编辑';
                            editButton.href = 'blog_edit.html?blogId=' + getURLParam();
                            navDiv.appendChild(editButton);
                        } 
                    } else {
                        alert("抱歉：后端程序不可用，请您稍后再试！");
                    }
                }
            });
          
        }
        function addRCount() {
            jQuery.ajax({
                url: "blog/updatereadcount",
                type: "POST",
                data: { "blogId": blogId },
                success: function (result) {
                    if (result != null && result.success == 200) {
                        console.log(result.data);
                    } else {
                        alert("后端程序不可用！1");
                    }
                }
            });
        }
        // function getURLParam(key) {
        //     var params = location.search;
        //     if (params.indexOf("?") >= 0) {
        //         params = params.substring(1);
        //         var paramArr = params.split('&');
        //         for (var i = 0; i < paramArr.length; i++) {
        //             var namevalues = paramArr[i].split("=");
        //             if (namevalues[0] == key) {
        //                 return namevalues[1];
        //             }
        //         }
        //     } else {
        //         return "";
        //     }
        // }
        function getURLParam() {
            let url = location.search;
            let p = url.split('?')[1]
            let params = new URLSearchParams(p)
            return params.get('blogId'); //1
        }

        function logout() {
            if (confirm("是否退出？")) {
                jQuery.ajax({
                    url: "user/logout",
                    type: "POST",
                    success: function (result) {
                        if (result != null && result.success == 200 && result.data == 1) {
                            alert("退出成功！");
                            location.href = "index.html";
                        } else {
                            alert("抱歉：操作出错，请重试！");
                        }
                    }
                });
                l
            }
        }
        function deleteBlog() {
            $.ajax({
                url: 'blog/deleteblog' + location.search,
                method: 'GET',
                success: function (result) {
                    if (result != null && result.succ == 200 && result.data == 1) {
                        alert("删除成功！");
                        location.href = "blog_list.html";
                    } else {
                        alert("抱歉：操作出错，请重试！");
                    }
                }
            })
        }
    </script>

</body>

</html>